/*
 * ConfigForm
 * 
 * Version information
 * 
 * Feb 25, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.webapp.struts.form;

import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import net.amsoft.iservice.isso.util.ISSOConstants;
import net.amsoft.iservice.isso.util.ISSOUtils;
import net.amsoft.iservice.util.exception.IServiceException;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * @author rahul
 * 
 */
@SuppressWarnings("serial")
public class ConfigForm extends ActionForm {
	private String sChallengeQuestion;

	private String sChallengeAnswer;

	private String sEmailAddress;

	private boolean bChangePassword;

	private String sOldPassword;

	private String sNewPassword;

	private String sConfirmPassword;

	private String sGreeting;

	private String sFirstName;

	private String sLastName;

	private boolean bChangeAnswer;

	private String sDupChallengeQuestion;

	private String sLoggedInSynonym;

	public boolean isChangePassword() {
		return bChangePassword;
	}

	public void setChangePassword(boolean changePassword) {
		bChangePassword = changePassword;
	}

	public String getChallengeQuestion() {
		return sChallengeQuestion;
	}

	public void setChallengeQuestion(String challengeQuestion) {
		sChallengeQuestion = challengeQuestion;
	}

	public String getChallengeAnswer() {
		return sChallengeAnswer;
	}

	public void setChallengeAnswer(String challengeAnswer) {
		if (challengeAnswer != null)
			challengeAnswer = challengeAnswer.trim();
		sChallengeAnswer = challengeAnswer;
	}

	public String getConfirmPassword() {
		return sConfirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		sConfirmPassword = confirmPassword;
	}

	public String getEmailAddress() {
		return sEmailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		if (emailAddress != null)
			emailAddress = emailAddress.trim();
		sEmailAddress = emailAddress;
	}

	public String getGreeting() {
		return sGreeting;
	}

	public void setGreeting(String greeting) {
		if (greeting != null)
			greeting = greeting.trim();
		sGreeting = greeting;
	}

	public String getOldPassword() {
		return sOldPassword;
	}

	public void setOldPassword(String password) {
		sOldPassword = password;
	}

	public String getNewPassword() {
		return sNewPassword;
	}

	public void setNewPassword(String passwd) {
		sNewPassword = passwd;
	}

	public String getFirstName() {
		return sFirstName;
	}

	public void setFirstName(String fname) {
		if (fname != null)
			fname = fname.trim();
		sFirstName = fname;
	}

	public String getLastName() {
		return sLastName;
	}

	public void setLastName(String lname) {
		if (lname != null)
			lname = lname.trim();
		sLastName = lname;
	}

	public boolean isChangeAnswer() {
		return bChangeAnswer;
	}

	public void setChangeAnswer(boolean changeAnswer) {
		bChangeAnswer = changeAnswer;
	}

	public String getDupChallengeQuestion() {
		return sDupChallengeQuestion;
	}

	public void setDupChallengeQuestion(String dupChallengeQuestion) {
		sDupChallengeQuestion = dupChallengeQuestion;
	}

	public String getLoggedInSynonym() {
		return sLoggedInSynonym;
	}

	public void setLoggedInSynonym(String loggedInSynonym) {
		sLoggedInSynonym = loggedInSynonym;
	}

	public ActionErrors validate(ActionMapping oMapping,
			HttpServletRequest oRequest) {
		ActionErrors oErrors = new ActionErrors();

		ResourceBundle oBundle = ResourceBundle.getBundle("issoResources",
				oRequest.getLocale());

		if ("save".equals(oRequest.getParameter("step"))) {
			if (invalid(sFirstName)) {
				oErrors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						ISSOConstants.ERR_INVALID, oBundle
								.getString(ISSOConstants.FIRST_NAME)));
			} else if (invalid(sLastName)) {
				oErrors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						ISSOConstants.ERR_INVALID, oBundle
								.getString(ISSOConstants.LAST_NAME)));
			} else if (invalid(sGreeting)) {
				oErrors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						ISSOConstants.ERR_INVALID, oBundle
								.getString(ISSOConstants.GREETING)));
			} else if (invalid(sEmailAddress)) {
				oErrors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						ISSOConstants.ERR_INVALID, oBundle
								.getString(ISSOConstants.EMAIL_ADDRESS)));
			} else if (bChangeAnswer) {
				if (invalidLength(sChallengeAnswer, 2, 20)) {
					oErrors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage(ISSOConstants.ERR_CHALLENEGE_ANSWER));
				}
			}

			if (oErrors.isEmpty() && bChangePassword == true) {
				if (invalidLength(sOldPassword, 4, 20)) {
					// display error "existing password may not be correct"
					oErrors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
							ISSOConstants.ERR_INVALID_PWD));
				} else {
					if (invalidLength(sNewPassword, 4, 20)) {
						oErrors.add(ActionErrors.GLOBAL_MESSAGE,
								new ActionMessage(
										ISSOConstants.ERR_PASSWD_LENGTH));
					} else if (!sNewPassword.equals(sConfirmPassword)) {
						oErrors.add(ActionMessages.GLOBAL_MESSAGE,
								new ActionMessage(
										ISSOConstants.ERR_PWD_NOT_SAME));
					}
				}
			}
		}

		// Set value of Challenge quest if validate fails
		if (!oErrors.isEmpty() && !isChangeAnswer()) {
			setChallengeQuestion(getDupChallengeQuestion());
			String sSpXri = ISSOConstants.getSPXri(oRequest.getServerName());
			oRequest.setAttribute(ISSOConstants.REQ_PARAM_SPXRI, sSpXri);
		}

		if (!oErrors.isEmpty()) {
			String sSpXri = ISSOConstants.getSPXri(oRequest.getServerName());
			oRequest.setAttribute(ISSOConstants.REQ_PARAM_SPXRI, sSpXri);
		}
		return oErrors;
	}

	private boolean invalid(String sValue) {
		// Validate e-mail address
		if (sValue.equals(sEmailAddress)) {
			try {
				if (ISSOUtils.checkEmailAddress(sValue) == 1)
					return false;
			} catch (IServiceException e) {
				return true;
			}
		}

		if (sValue == null || sValue.trim().length() == 0)
			return true;
		else
			return false;
	}

	private boolean invalidLength(String sValue, int minLength, int maxLength) {
		if (sValue == null || sValue.length() < minLength
				|| sValue.length() > maxLength)
			return true;
		else
			return false;
	}

}